package com.invengo.module_check.ui.activity.main;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.invengo.base.excel.ImportExcelUtils;
import com.invengo.base.router.RouterActivityPath;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.BundleKey;
import com.invengo.module_check.databinding.ActivityMainBinding;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.ToastUtils;

import static me.goldze.mvvmhabit.utils.constant.Constants.TOKEN_ADD_BOOKS;
import static me.goldze.mvvmhabit.utils.constant.Constants.TOKEN_CAMERA;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

/**
 * @author buoker
 * @version 1.0.0
 * @date 2023/10/24
 * @className java.lang.String
 */
@Route(path = RouterActivityPath.Check.PAGER_MAIN)
public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewMode> {
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void initData() {
        switchLanguage(Locale.ENGLISH);
    }

    private void switchLanguage(Locale locale) {
        //设置应用语言类型
        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        config.setLocale(locale);
        resources.updateConfiguration(config, dm);
    }


    @Override
    public void initNavgation() {
        NavController navController = Navigation.findNavController(this, R.id.main_fragmentcontainerview);
        AppBarConfiguration appBarConfiguration =
                new AppBarConfiguration.Builder(navController.getGraph()).build();
        NavigationUI.setupWithNavController(
                binding.toolbar, navController, appBarConfiguration);
        navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
            if (arguments != null) {
                String title = arguments.getString(BundleKey.KEY_TITLE);
                if (!TextUtils.isEmpty(title))
                    binding.toolbar.setTitle(title);
            }
        });
    }

    @Override
    public void initParam() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    // Get the results:
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 10086:
                try {
                    if (data == null | data.getData() == null) {
                        ToastUtils.showShort("文件不存在，请重新选择导入表格！");
                        return;
                    }
                    Uri uri = data.getData();
                    InputStream inputStream = null;

                    inputStream = getContentResolver().openInputStream(uri);
                    List<String> keywords = ImportExcelUtils.readExcelForSearch(inputStream);
                    if (keywords == null | keywords.isEmpty()) {
                        ToastUtils.showShort("文件数据空，请添加数据");
                    } else {
                        for (String keyword : keywords) {
                            Messenger.getDefault().send(keyword, TOKEN_ADD_BOOKS);
                        }
                    }
                } catch (Exception e) {
                    ToastUtils.showShort("待查询表格未找到，请添加数据！");
                }
                break;
            default:
                if (result != null) {
                    if (result.getContents() == null) {
                    } else {
                        Messenger.getDefault().send(result.getContents(), TOKEN_CAMERA);
                    }
                }
                break;
        }
    }

}
